package org.example.controller.utils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.model.common.ResponseResult;
import org.example.model.util.dtos.md.MdEditFileDto;
import org.example.model.util.dtos.md.MdFileContentDto;
import org.example.model.util.vos.md.MdFileContentVo;
import org.example.model.util.vos.md.ModifyMdEditFileVo;
import org.example.controller.common.permission.PermissionControl;
import org.example.service.util.md.MdEditFileService;
import org.example.service.util.md.MdFileContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * md编辑器表现层
 */
@CrossOrigin
@RestController
@RequestMapping("/md-edit")
@Api(value = "MD编辑器接口", tags = {"MD编辑器接口"})
@PermissionControl(Parent = "常用工具", Name = "MD编辑器", Order = 1)
public class MdFileEditController {

    @Autowired
    private MdEditFileService mdEditFileService;

    @Autowired
    private MdFileContentService mdFileContentService;

    /**
     * 添加一条md编辑器文件信息
     */
    @PostMapping("/file-info")
    @ApiOperation("添加一条md编辑器文件信息")
    @PermissionControl(Parent = "MD编辑器", Name = "添加", Order = 1)
    public ResponseResult<Boolean> InsertMdEditFile(@RequestBody ModifyMdEditFileVo modifyMdEditFileVo){
        return mdEditFileService.InsertMdEditFile(modifyMdEditFileVo);
    }

    /**
     * 根据文件id修改一条md编辑器文件信息
     */
    @PutMapping("/file-info")
    @ApiOperation("根据文件id修改一条md编辑器文件信息")
    @PermissionControl(Parent = "MD编辑器", Name = "重命名", Order = 3)
    public ResponseResult<Boolean> UpdateMdEditFileById(@RequestBody ModifyMdEditFileVo modifyMdEditFileVo){
        return mdEditFileService.UpdateMdEditFileById(modifyMdEditFileVo);
    }

    /**
     * 根据文件id删除一条md编辑器文件信息
     */
    @DeleteMapping("/file-info/{id}")
    @ApiOperation("根据文件id删除一条md编辑器文件信息")
    @PermissionControl(Parent = "MD编辑器", Name = "删除", Order = 2)
    public ResponseResult<Boolean> DeleteMdEditFileById(@PathVariable Integer id){
        return mdEditFileService.DeleteMdEditFileById(id);
    }

    /**
     * 获取md文件树
     */
    @GetMapping("/file-tree")
    @ApiOperation("获取md文件树")
    @PermissionControl(Parent = "MD编辑器", Name = "获取md文件树", Order = 4)
    public ResponseResult<List<MdEditFileDto>> GetMdEditFileTree(){
        return mdEditFileService.GetMdEditFileTree();
    }

    /**
     * 根据文件id修改文件内容
     */
    @PutMapping("/file-content")
    @ApiOperation("根据文件id修改文件内容")
    @PermissionControl(Parent = "MD编辑器", Name = "修改文件内容", Order = 5)
    public ResponseResult<Boolean> UpdateMdFileContentById(@RequestBody MdFileContentVo mdFileContentVo) {
        return mdFileContentService.UpdateMdFileContentById(mdFileContentVo);
    }

    /**
     * 根据文件id获取文件内容
     */
    @GetMapping("/file-content/{fileId}")
    @ApiOperation("根据文件id获取文件内容")
    @PermissionControl(Parent = "MD编辑器", Name = "获取文件内容", Order = 6)
    public ResponseResult<MdFileContentDto> SelectMdFileContentByFileId (@PathVariable Integer fileId) {
        return mdFileContentService.SelectMdFileContentByFileId(fileId);
    }
}
